package com.xhtt.modules.inspection.util;

import com.xhtt.common.utils.DateUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2019/3/1 11:40
 **/
public class InspectDateUtil {

    public static String replaceNum(int index) {

        String[] str = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
        return str[index - 1] + "月";
    }

    /**
     * 获取当前日期所在的周一和周日
     *
     * @return [星期一, 星期日]
     */
    public static List<LocalDateTime> getCurrentWeek(LocalDateTime dateTime) {

        List<LocalDateTime> result = new ArrayList<>();

        if (dateTime == null) {
            dateTime = DateUtils.now();

        }
        int weekOfDay = dateTime.getDayOfWeek().getValue();
        // 星期一
        LocalDateTime mon = dateTime.plusDays(-(weekOfDay - 1));
        // 星期日
        LocalDateTime sun = dateTime.plusDays(7 - weekOfDay);

        result.add(0, mon);
        result.add(1, sun);

        return result;
    }

    /**
     * 获取当前日期所在的月份的第一天和最后一天
     *
     * @return [一号, 最后一天]
     */
    public static List<LocalDateTime> getCurrentMonth(LocalDateTime dateTime) {

        List<LocalDateTime> result = new ArrayList<>();

        if (dateTime == null) {
            dateTime = DateUtils.now();
        }

        int dayOfMonth = dateTime.getDayOfMonth();

        // 这个月多少天
        int countOfMonth = dateTime.getMonth().length(dateTime.toLocalDate().isLeapYear());

        // 这个月第一天
        LocalDateTime first = dateTime.plusDays(-(dayOfMonth - 1));

        // 这个月最后一天
        LocalDateTime last = dateTime.plusDays(countOfMonth - dayOfMonth);

        result.add(0, first);
        result.add(1, last);

        return result;
    }

}
